package com.icode.core.dto.user;

import com.icode.core.model.user.*;
import com.icode.core.shared.JodaTimeUtils;
import com.icode.core.shared.SecurityUtil;
import com.icode.core.shared.StringUtils;
import org.hibernate.validator.constraints.NotEmpty;
import org.joda.time.LocalDate;
import org.springframework.web.multipart.MultipartFile;

import java.util.ArrayList;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: xianfei
 * Date: 15-1-5
 * Time: 下午6:28
 * To change this template use File | Settings | File Templates.
 */
public class UsersInfoDTO {

    @NotEmpty(message = "请输入用户名!")
    private String username;
    @NotEmpty(message = "请输入真实姓名!")
    private String realName;
    @NotEmpty(message = "请选择性别!")
    private String gender;
    @NotEmpty(message = "请选择生日!")
    private String birthday;
    private String IDCard;
    private String phoneNum;
    private String email;
    private String province;
    private String city;
    private String district;
    private String area;
    private String position;
    private List<ProvinceDTO> provinces;
    private List<CityDTO> cities;
    private List<DistrictDTO> districts;
    private MultipartFile usersInfoImage;
    private String imageGuid;

    //Add new variable by zhisong.hu in December 22,2015
    private String provinceName;
    private String cityName;
    private String districtName;
    private String activate;

    public UsersInfoDTO() {
    }

    public UsersInfoDTO(String username, String realName, String gender, String birthday, String IDCard, String phoneNum, String email, String province, String city, String district, String area, String position, String imageGuid, String provinceName, String cityName, String districtNamed, String isActivate) {
        this.username = username;
        this.realName = realName;
        this.gender = gender;
        this.birthday = birthday;
        this.IDCard = IDCard;
        this.phoneNum = phoneNum;
        this.email = email;
        this.province = province;
        this.city = city;
        this.district = district;
        this.area = area;
        this.position = position;
        this.imageGuid = imageGuid;
        this.provinceName = provinceName;
        this.cityName = cityName;
        this.districtName = districtNamed;
        this.activate = isActivate;
    }

    public UserInfo toUsersInfo(UserInfo userInfo, Province province, City city, District district) {
        LocalDate localDate = null;
        if (this.birthday != null) {
            localDate = JodaTimeUtils.StringToLocalDate(this.birthday);
        }
        Gender gender = null;
        String position = "";
        if (!StringUtils.isEmpty(this.gender)) {
            gender = Gender.valueOf(this.gender);
        }
        if (!StringUtils.isEmpty(this.position)) {
            position = this.position;
        }
        User user = SecurityUtil.currentUser();
        if (userInfo != null) {
            userInfo.update(this.username, this.realName, gender, localDate, this.IDCard, this.email, province, city, district, this.area, position);
        } else {
            userInfo = new UserInfo(this.username, this.realName, gender, localDate, this.IDCard, user, this.email, province, city, district, this.area, position);
            userInfo.initUser(user);
            user.initUserInfo(userInfo);
        }
        return userInfo;

    }

    public void initAddress(List<ProvinceDTO> provinces, List<CityDTO> cities, List<DistrictDTO> districts) {
        this.provinces = provinces;
        this.cities = cities;
        this.districts = districts;
    }

    public void initProvinces(List<ProvinceDTO> provinces) {
        this.provinces = provinces;
    }

    public void initCities(List<CityDTO> cities) {
        this.cities = cities;
    }

    public void initDistricts(List<DistrictDTO> districts) {
        this.districts = districts;
    }

    public static UsersInfoDTO toDTO(UserInfo usersInfo) {
        String username1 = "";
        String position = "";
        String email = "";
        String gender = "";
        String birthday = "";
        String imageGuid = "";
        String province = "";
        String city = "";
        String district = "";
        String provinceName = "";
        String cityName = "";
        String districtName = "";
        String isActivate = "";
        if (!StringUtils.isEmpty(usersInfo.username())) {
            username1 = usersInfo.username();
        }
        if (!StringUtils.isEmpty(usersInfo.position())) {
            position = usersInfo.position();
        }
        if (!StringUtils.isEmpty(usersInfo.email())) {
            email = usersInfo.email();
        }
        if (usersInfo.gender() != null) {
            gender = usersInfo.gender().name();
        }
        if (usersInfo.birthday() != null) {
            birthday = String.valueOf(usersInfo.birthday());
        }
        if (usersInfo.usersInfoImage() != null) {
            imageGuid = usersInfo.usersInfoImage().guid();
        }
        String realName = usersInfo.realName();
        String IDCard = usersInfo.IDCard();
        String phoneNum = usersInfo.user().getUsername();
        if (usersInfo.province() != null) {
            province = String.valueOf(usersInfo.province().id());
            provinceName = String.valueOf(usersInfo.province().name());
        }
        if (usersInfo.city() != null) {
            city = String.valueOf(usersInfo.city().id());
            cityName = String.valueOf(usersInfo.city().name());
        }
        if (usersInfo.district() != null) {
            district = String.valueOf(usersInfo.district().id());
            districtName = String.valueOf(usersInfo.district().name());
        }
        isActivate = usersInfo.user().isActivate();
        String area = usersInfo.area();
        UsersInfoDTO usersInfoDTO = new UsersInfoDTO(username1, realName, gender, birthday, IDCard, phoneNum, email, province, city, district, area, position, imageGuid, provinceName, cityName, districtName, isActivate);
        return usersInfoDTO;
    }

    public static List<UsersInfoDTO> toDTOs(List<UserInfo> userInfos) {
        List<UsersInfoDTO> usersInfoDTOs = new ArrayList<>();
        for (UserInfo u : userInfos) {
            UsersInfoDTO usersInfoDTO = UsersInfoDTO.toDTO(u);
            usersInfoDTOs.add(usersInfoDTO);
        }
        return usersInfoDTOs;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public void setRealName(String realName) {
        this.realName = realName;
    }

    public void setGender(String gender) {
        this.gender = gender;
    }

    public void setIDCard(String IDCard) {
        this.IDCard = IDCard;
    }

    public void setPhoneNum(String phoneNum) {
        this.phoneNum = phoneNum;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public void setPosition(String position) {
        this.position = position;
    }

    public List<ProvinceDTO> getProvinces() {
        return provinces;
    }

    public List<CityDTO> getCities() {
        return cities;
    }

    public List<DistrictDTO> getDistricts() {
        return districts;
    }

    public void setProvinces(List<ProvinceDTO> provinces) {
        this.provinces = provinces;
    }

    public void setCities(List<CityDTO> cities) {
        this.cities = cities;
    }

    public void setDistricts(List<DistrictDTO> districts) {
        this.districts = districts;
    }

    public String getUsername() {
        return username;
    }

    public String getBirthday() {
        return birthday;
    }

    public void setBirthday(String birthday) {
        this.birthday = birthday;
    }

    public String getRealName() {
        return realName;
    }

    public String getGender() {
        return gender;
    }

    public String getIDCard() {
        return IDCard;
    }

    public String getPhoneNum() {
        return phoneNum;
    }

    public String getEmail() {
        return email;
    }

    public String getPosition() {
        return position;
    }

    public MultipartFile getUsersInfoImage() {
        return usersInfoImage;
    }

    public void setUsersInfoImage(MultipartFile usersInfoImage) {
        this.usersInfoImage = usersInfoImage;
    }

    public String getProvince() {
        return province;
    }

    public String getCity() {
        return city;
    }

    public String getDistrict() {
        return district;
    }

    public String getArea() {
        return area;
    }


    public void setProvince(String province) {
        this.province = province;
    }

    public void setCity(String city) {
        this.city = city;
    }

    public void setDistrict(String district) {
        this.district = district;
    }

    public void setArea(String area) {
        this.area = area;
    }

    public String getImageGuid() {
        return imageGuid;
    }

    public void setImageGuid(String imageGuid) {
        this.imageGuid = imageGuid;
    }

    public String getProvinceName() {
        return provinceName;
    }

    public void setProvinceName(String provinceName) {
        this.provinceName = provinceName;
    }

    public String getCityName() {
        return cityName;
    }

    public void setCityName(String cityName) {
        this.cityName = cityName;
    }

    public String getDistrictName() {
        return districtName;
    }

    public void setDistrictName(String districtName) {
        this.districtName = districtName;
    }

    public String getActivate() {
        return activate;
    }

    public void setActivate(String activate) {
        this.activate = activate;
    }
}
